package controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import model.WebHistoryDataException;
import model.WebHistoryModel;
import view.Console;
import view.WebHistoryException;
import view.WebHistoryView;
import visualization.PairEntry;


/**
 * Takes input from the console and modifies the WebHistoryView and
 * WebHistoryModel based on these inputs.
 * 
 * @author Ben Schwab
 * 
 */

public class CommandProcessor implements
        Commandable<WebHistoryView, WebHistoryModel> {

    private WebHistoryView myViewGUI;
    private WebHistoryModel myWebsiteModel;
    private String myLoadCommand = "load";
    private String myCreateCommand = "create";
    private String myAddTitleCommand = "add title";

    /**
     * Instantiation of CommandProcessor; does not make sense to implement.
     */
    public CommandProcessor () {
    }

    /**
     * Process any command from the console input field.
     * 
     * @param command command
     */
    public void processCommand (String command) {
        if (command.startsWith(myLoadCommand)) {
            loadWebHistoryModel(command);
        }
        else if (command.startsWith(myCreateCommand)) {
            createVisualization(command);
        }
        else if (command.startsWith("randomizecolors")) {
            myViewGUI.randomizeColors();
        }
        else if (command.startsWith(myAddTitleCommand)) {
            String[] title = command.split(myAddTitleCommand);
            myViewGUI.setVisualizationTitle(title[1]);
        }
        else{
            Console.appendConsoleText("Command not found");
        }
    }

    /**
     * Load the Model using the data file contained in the given command.
     * 
     * @param command a string containing the load command
     */

    public void loadWebHistoryModel (String command) {
        String[] strSplit = command.split(" ");
        if (myLoadCommand.equals(strSplit[0]) && strSplit[1] != null) {
            File dataFile = new File(strSplit[1]);
            try {
                myWebsiteModel.importData(dataFile);
            }
            catch (WebHistoryDataException e) {
                Console.appendConsoleText("error: " + e.getMessage());
            }

        }
    }

    /**
     * Create the visualization specified by the user input given in the
     * commandString.
     * 
     * @param commandString taken from user input into the console window
     */
    public void createVisualization (String commandString) {
        String[] strSplit = commandString.split(" ");
        if (!myCreateCommand.equals(strSplit[0]) || strSplit[1] == null || strSplit[2] == null) {
            Console.appendConsoleText(
                    "specify type of visualization. See help menu for more details.");
        }
        String viewType = strSplit[1];
        try {
            List<PairEntry<String, Double>> pairedData = generatePairList(commandString);
            System.out.println(viewType);
            myViewGUI.createPairedVisualization(viewType, pairedData);
        }
        catch (WebHistoryException e) {
            Console.appendConsoleText(e.getMessage());
        }

    }

    @Override
    public void setView (WebHistoryView viewGUI) {
        myViewGUI = viewGUI;
    }

    @Override
    public void setModel (WebHistoryModel websiteModel) {
        myWebsiteModel = websiteModel;

    }

    private PairEntry<String, Double> generatePair (String websiteName,
            String input) throws WebHistoryException {
        String[] inputSplit = input.split(" ");
        if (inputSplit[2] == null) { return null; }
        double d = Double.POSITIVE_INFINITY;
        if ("numvisits".equals(inputSplit[2].toLowerCase())) {
            d = myWebsiteModel.getNumVisits(websiteName);
        }
        else if ("timespent".equals(inputSplit[2].toLowerCase())) {
            d = myWebsiteModel.timeSpentOnWebsite(websiteName);
        }
        else {
            throw new WebHistoryException("invalid data type");
        }
        return new PairEntry<String, Double>(websiteName, d);
    }

    private List<PairEntry<String, Double>> generatePairList (
            String commandString) throws WebHistoryException {
        List<PairEntry<String, Double>> pairList = new ArrayList<PairEntry<String, Double>>();
        List<String> allWebsites = myWebsiteModel.getAllWebsites();
        for (String websiteName : allWebsites) {
            pairList.add(generatePair(websiteName, commandString));
        }
        return pairList;
    }

}
